package com.nageoffer.jobtrain.projects.service.handler.remind.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nageoffer.jobtrain.projects.config.AliYunSendMessageConfig;
import com.nageoffer.jobtrain.projects.dao.entity.ContestDO;
import com.nageoffer.jobtrain.projects.dao.mapper.ContestMapper;
import com.nageoffer.jobtrain.projects.service.handler.remind.ContestRemindTemplate;
import com.nageoffer.jobtrain.projects.service.handler.remind.dto.ContestRemindDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户手机短信号通知实现类
 * author：唐晨
 */
@Component
@RequiredArgsConstructor
public class SendSmsRemindContestTemplate implements ContestRemindTemplate {
    private final AliYunSendMessageConfig aliYunSendMessageConfig;
    private final ContestMapper contestMapper;
    /**
     * 签名名称
     */
    @Value("${sms.aliyun.signName}")
    private String signName;

    /**
     * 模板code
     */
    @Value("${sms.aliyun.contestBeginTemplateCode}")
    private String contestBeginTemplateCode;

    /**
     * 以手机短信的方式进行通知
     *
     * @param requestParam 提醒请求入参
     */
    @Override
    public boolean remind(ContestRemindDTO requestParam) {
        LambdaQueryWrapper<ContestDO> queryWrapper = Wrappers.lambdaQuery(ContestDO.class)
                .eq(ContestDO::getId, requestParam.getContestId())
                .eq(ContestDO::getDelFlag, 0);
        ContestDO contestDO = contestMapper.selectOne(queryWrapper);
        if (contestDO != null) {
            Map<String, String> paramsMap = new HashMap<>();
            paramsMap.put("title", contestDO.getTitle());
            paramsMap.put("time", requestParam.getRemindTime().toString());
            String params = JSON.toJSONString(paramsMap);
            return aliYunSendMessageConfig.sendMessageTemplate(requestParam.getPhoneNumber(), signName, contestBeginTemplateCode, params);
        }
        return false;
    }
}
